package com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.entity.ErpPurchaseorder;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.entity.ErpPurchaseorderentry;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.mapper.ErpPurchaseorderentryMapper;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.dto.ErpPurchaseorderListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;

import java.util.List;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.mapper.ErpPurchaseorderMapper;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.service.IErpPurchaseorderService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-10-13
 */
@Service
@AllArgsConstructor
public class ErpPurchaseorderServiceImpl extends BaseService<ErpPurchaseorderMapper, ErpPurchaseorder> implements IErpPurchaseorderService {

	private ErpPurchaseorderentryMapper erpPurchaseorderentryMapper;

	@Override
	public IPage<ErpPurchaseorder> getPageList(ErpPurchaseorderListDto pageListDto) {
		Wrapper<ErpPurchaseorder> wrapper = Wrappers.<ErpPurchaseorder>query().lambda()				.eq(!StringUtil.isEmpty(pageListDto.getF_PurchaseOrder()), ErpPurchaseorder::getFPurchaseorder, pageListDto.getF_PurchaseOrder())
				.ge(!StringUtil.isEmpty(pageListDto.getF_PurchaseDate_Start()), ErpPurchaseorder::getFPurchasedate, pageListDto.getF_PurchaseDate_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_PurchaseDate_End()), ErpPurchaseorder::getFPurchasedate, pageListDto.getF_PurchaseDate_End())
				.like(!StringUtil.isEmpty(pageListDto.getF_PurchasePeople()), ErpPurchaseorder::getFPurchasepeople, pageListDto.getF_PurchasePeople())
				.eq(!StringUtil.isEmpty(pageListDto.getF_DeliveryUnit()), ErpPurchaseorder::getFDeliveryunit, pageListDto.getF_DeliveryUnit())
				.eq(!StringUtil.isEmpty(pageListDto.getF_PurchaseType()), ErpPurchaseorder::getFPurchasetype, pageListDto.getF_PurchaseType())
				.ge(!StringUtil.isEmpty(pageListDto.getF_DeliveryDate_Start()), ErpPurchaseorder::getFDeliverydate, pageListDto.getF_DeliveryDate_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_DeliveryDate_End()), ErpPurchaseorder::getFDeliverydate, pageListDto.getF_DeliveryDate_End());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addErpPurchaseorder(ErpPurchaseorder erpPurchaseorder, List<ErpPurchaseorderentry> erpPurchaseorderentryList) {
		String erpPurchaseorderId = IdWorker.get32UUID();
		erpPurchaseorder.setFId(erpPurchaseorderId);
		erpPurchaseorderentryList.forEach(erpPurchaseorderentry -> erpPurchaseorderentry.setFOrderid(erpPurchaseorderId));
		this.saveBatch(erpPurchaseorderentryList, ErpPurchaseorderentry.class, ErpPurchaseorderentryMapper.class);
		boolean isSuccess = this.save(erpPurchaseorder);
		return isSuccess ? erpPurchaseorderId : null;
	}

	@Override
	public boolean updateErpPurchaseorder(String id, ErpPurchaseorder erpPurchaseorder, List<ErpPurchaseorderentry> erpPurchaseorderentryList) {
		erpPurchaseorder.setFId(id);
		erpPurchaseorderentryList.forEach(erpPurchaseorderentry -> erpPurchaseorderentry.setFOrderid(id));
		erpPurchaseorderentryMapper.delete(Wrappers.<ErpPurchaseorderentry>query().lambda().eq(ErpPurchaseorderentry::getFOrderid, id));
		this.saveBatch(erpPurchaseorderentryList, ErpPurchaseorderentry.class, ErpPurchaseorderentryMapper.class);
		return this.updateById(erpPurchaseorder);
	}

	public List<ErpPurchaseorderentry> getErpPurchaseorderentryByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpPurchaseorderentry>query().lambda().eq(ErpPurchaseorderentry::getFOrderid, parentId);
		return erpPurchaseorderentryMapper.selectList(wrapper);
	}
}
